// 通用功能模块
document.addEventListener('DOMContentLoaded', function() {
    // 页面过渡效果
    const pageTransition = document.createElement('div');
    pageTransition.className = 'page-transition';
    document.body.appendChild(pageTransition);
    
    // 页面加载时的过渡
    setTimeout(() => {
        pageTransition.style.transform = 'translateY(0)';
        setTimeout(() => {
            pageTransition.style.transform = 'translateY(-100%)';
        }, 300);
    }, 100);
    
    // 为所有链接添加过渡效果
    document.querySelectorAll('a[href]').forEach(link => {
        // 排除锚点链接和外部链接
        if (!link.href.includes('#') && link.hostname === window.location.hostname) {
            link.addEventListener('click', function(e) {
                e.preventDefault();
                const targetUrl = this.href;
                
                // 显示过渡层
                pageTransition.style.transform = 'translateY(0)';
                
                // 延迟跳转
                setTimeout(() => {
                    window.location.href = targetUrl;
                }, 300);
            });
        }
    });
    
    // 创建装饰元素
    createDecorElements();
    
    // 初始化音频控制
    initAudioControl();
});

// 创建装饰元素
function createDecorElements() {
    const colors = ['var(--primary)', 'var(--secondary)', 'var(--accent)', 'var(--purple)'];
    const count = 3;
    
    for (let i = 0; i < count; i++) {
        const decor = document.createElement('div');
        decor.className = 'decor';
        
        // 随机样式
        decor.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
        const size = Math.random() * 80 + 50;
        decor.style.width = `${size}px`;
        decor.style.height = `${size}px`;
        
        // 随机位置
        decor.style.top = `${Math.random() * 80 + 10}%`;
        decor.style.left = `${Math.random() * 80 + 10}%`;
        
        // 随机动画延迟
        decor.style.animationDelay = `${Math.random() * 2}s`;
        
        document.body.appendChild(decor);
    }
}

// 初始化音频控制
function initAudioControl() {
    // 创建音频元素
    const audio = document.createElement('audio');
    audio.id = 'background-music';
    audio.src = 'audio/birthday_music.mp3';
    audio.loop = true;
    audio.volume = 0.3;
    document.body.appendChild(audio);
    
    // 创建音频控制按钮
    const audioControl = document.createElement('div');
    audioControl.className = 'audio-control';
    audioControl.innerHTML = '<i class="fa fa-music text-primary"></i>';
    document.body.appendChild(audioControl);
    
    // 音频状态
    let isPlaying = false;
    
    // 尝试自动播放
    audio.play().then(() => {
        isPlaying = true;
    }).catch(e => {
        console.log('需要用户交互才能播放音乐:', e);
    });
    
    // 控制按钮点击事件
    audioControl.addEventListener('click', function() {
        if (isPlaying) {
            audio.pause();
            audioControl.innerHTML = '<i class="fa fa-volume-off text-gray"></i>';
        } else {
            audio.play();
            audioControl.innerHTML = '<i class="fa fa-music text-primary"></i>';
        }
        isPlaying = !isPlaying;
    });
    
    // 暴露音频控制函数
    window.toggleAudio = function() {
        audioControl.click();
    };
}

// 加载JSON数据
function loadJSON(url) {
    return fetch(url)
        .then(response => {
            if (!response.ok) {
                throw new Error('数据加载失败: ' + response.status);
            }
            return response.json();
        })
        .catch(error => {
            console.error('加载JSON失败:', error);
            alert('数据加载失败，请刷新页面重试');
            return null;
        });
}

// 创建礼花效果
function createConfetti(count, containerId = 'particle-container') {
    const container = document.getElementById(containerId) || document.body;
    if (!container) return;
    
    const colors = ['#FF6B9B', '#6BCBFF', '#FFD66B', '#A78BFA', '#6BFFB8', '#FFFFFF'];
    
    for (let i = 0; i < count; i++) {
        const confetti = document.createElement('div');
        confetti.className = 'confetti';
        
        // 随机样式
        confetti.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
        const size = Math.random() * 8 + 4;
        confetti.style.width = `${size}px`;
        confetti.style.height = `${size}px`;
        
        // 随机位置
        confetti.style.left = `${Math.random() * 100}vw`;
        
        // 随机动画
        const duration = Math.random() * 8 + 4;
        confetti.style.animation = `confetti-fall ${duration}s linear forwards`;
        
        container.appendChild(confetti);
        
        // 动画结束后移除
        setTimeout(() => {
            if (container.contains(confetti)) {
                container.removeChild(confetti);
            }
        }, duration * 1000);
    }
}

// 显示提示消息
function showToast(message, duration = 3000) {
    // 检查是否已有toast
    let toast = document.querySelector('.toast');
    
    if (!toast) {
        // 创建toast元素
        toast = document.createElement('div');
        toast.className = 'toast';
        document.body.appendChild(toast);
    }
    
    // 设置消息并显示
    toast.textContent = message;
    toast.classList.add('show');
    
    // 自动隐藏
    setTimeout(() => {
        toast.classList.remove('show');
    }, duration);
}
